﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NBusiness.ESharp.Compiler;
using NBusiness.ESharp.Testing.Mocks;
using NBusiness.CodeDom.Compiler;
using NBusiness.CodeDom;
using NBusiness.ESharp.Testing.Properties;
using NBusiness.CodeDom.Services;

namespace NBusiness.ESharp.Testing
{
    [TestClass]
    public class AuthorizeRuleTests
    {
        [TestMethod]
        public void TestAuthorizationRules1()
        {
            ESharpProvider provider = new ESharpProvider();
            MockBuildErrorService errorService = new MockBuildErrorService();
            MockTypeFinder typeFinderService = new MockTypeFinder();
            provider.SetService(typeof(IBuildErrorService), errorService);

            EntityRoot root = provider.GetParser().ParseFiles(
                new EntityFile("Rules.Authorize1.es", Resources.Rules_Authorize1));

            Assert.AreEqual(0, errorService.Errors.Count);

            EntityFamily family = root.Families["Business"];
            Assert.IsNotNull(family);
            Assert.AreEqual(1, family.Entities.Count);

            Entity simple = family.Entities[0];
            Assert.IsNotNull(simple);
            Assert.AreEqual(4, simple.Rules.Count);

            Assert.IsInstanceOfType(simple.Rules[0], typeof(EntityAuthorization));
            Assert.IsInstanceOfType(simple.Rules[1], typeof(EntityAuthorization));
            Assert.IsInstanceOfType(simple.Rules[2], typeof(EntityAuthorization));
            Assert.IsInstanceOfType(simple.Rules[3], typeof(EntityAuthorization));

            Assert.IsTrue((simple.Rules[0] as EntityAuthorization).Allow);
            Assert.IsTrue((simple.Rules[1] as EntityAuthorization).Allow);
            Assert.IsFalse((simple.Rules[2] as EntityAuthorization).Allow);
            Assert.IsTrue((simple.Rules[3] as EntityAuthorization).Allow);

            Assert.AreEqual(EntityAuthorizationMethod.Fetch, (simple.Rules[0] as EntityAuthorization).Method);
            Assert.AreEqual(EntityAuthorizationMethod.Insert, (simple.Rules[1] as EntityAuthorization).Method);
            Assert.AreEqual(EntityAuthorizationMethod.Update, (simple.Rules[2] as EntityAuthorization).Method);
            Assert.AreEqual(EntityAuthorizationMethod.Delete, (simple.Rules[3] as EntityAuthorization).Method);
        }
    }
}
